package com.hpeng.sys.action;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import com.hpeng.gl.service.StaffService;
import com.hpeng.sys.model.Department;
import com.hpeng.sys.model.Staff;
import com.hpeng.sys.model.SysTemplate;
import com.hpeng.sys.model.SysUser;
import com.hpeng.sys.service.DepartmentsMgr;
import com.hpeng.sys.service.Function;
import com.hpeng.sys.service.StaMgr;
import com.hpeng.sys.service.SysTemplateMgr;
import com.hpeng.sys.service.SysUserMgr;
import com.hpeng.sys.vo.Lookup;
import com.hpeng.sys.vo.FuncProperties;
import com.hpeng.util.MD5;
import com.hpeng.util.Pagination;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@Component("SysUserAction")
@Scope("prototype")
public class UserAction extends ActionSupport implements ModelDriven<SysUser> {
	public Pagination<SysUser> pagination;
	private String sas;
	private Staff staff;
	private Department department;
	private SysTemplate sysTemplate;
	private String staffname;
	private String templatesname;
	private String Departmentsname;
	private String password;
	private String userName;
	private String msg;
	private String un;
	private String uc;
	private String qm;
	private String gc;
	public String getSas() {
		return sas;
	}
	public void setSas(String sas) {
		this.sas = sas;
	}
	public void setGc(String gc) {
		this.gc = gc;
	}
	public String getGc() {
		return gc;
	}
	public Department getDepartment() {
		return department;
	}
	public void setDepartment(Department department) {
		this.department = department;
	}
	public String getStaffname() {
		return staffname;
	}
	public void setStaffname(String staffname) {
		this.staffname = staffname;
	}
	public String getTemplatesname() {
		return templatesname;
	}
	public void setTemplatesname(String templatesname) {
		this.templatesname = templatesname;
	}
	public String getDepartmentsname() {
		return Departmentsname;
	}
	public void setDepartmentsname(String departmentsname) {
		Departmentsname = departmentsname;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	private SysUser sysUser;
	private int page;
	public String pageLink;
	public String navStr;
	private String FN;
	private String opt;
	public List<Department> departments;
	public List<Lookup> accessRightData;
	public List<FuncProperties> sysFuncData;
	public Map<String,String> tmpltAuthMap;
	public List<SysUser> sysUsers;
	public List<SysTemplate> sysTemplates;
	public List<Staff> staffs;
	private List<String> deleteUsers;
	public List<String> getDeleteUsers() {
		return deleteUsers;
	}
	public void setDeleteUsers(List<String> deleteUsers) {
		this.deleteUsers = deleteUsers;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public String getUc() {
		return uc;
	}
	public void setUc(String uc) {
		this.uc = uc;
	}
	public List<Department> getDepartments() {
		return departments;
	}
	public void setDepartments(List<Department> departments) {
		this.departments = departments;
	}
	public List<Staff> getStaffs() {
		return staffs;
	}
	public void setStaffs(List<Staff> staffs) {
		this.staffs = staffs;
	}
	public String getFN() {
		return FN;
	}
	public void setFN(String FN) {
		this.FN = FN;
	}

	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}

	public String getOpt() {
		return opt;
	}
	public void setOpt(String opt) {
		this.opt = opt;
	}
	public List<SysTemplate> getSysTemplates(){
		return this.sysTemplates;
	}
	public void setSysTemplates(List<SysTemplate> sysTemplates){
		this.sysTemplates = sysTemplates;
	}
	public SysUser SysUser = new SysUser();
	public SysUser getModel() {
		return SysUser;
	}
	public HttpServletRequest request(){
		return ServletActionContext.getRequest();
	}
	public Map<String,Object> session(){
		return ActionContext.getContext().getSession();
	}
	public void setStaffService(StaffService staffService) {
		this.staffService = staffService;
	}
	public StaffService getStaffService() {
		return staffService;
	}
	public void setSysUser(SysUser sysUser) {
		this.sysUser = sysUser;
	}
	public SysUser getSysUser() {
		return sysUser;
	}
	public String getUn() {
		return un;
	}
	public void setUn(String un) {
		this.un = un;
	}
	public void setQm(String qm) {
		this.qm = qm;
	}
	public String getQm() {
		return qm;
	}
	public void setDepid(String depid) {
		this.depid = depid;
	}
	public String getDepid() {
		return depid;
	}
	public void setTmpCode(String tmpCode) {
		this.tmpCode = tmpCode;
	}
	public String getTmpCode() {
		return tmpCode;
	}
	public String list() {
		this.pagination = sysUserMgr.getPagination(page);
		this.pageLink = pagination.getPageLink("tmplt-list?FN=" + FN);
		this.navStr = function.getNavStr(FN);
		return "list";
	}

	public String oprt() {
		this.navStr = function.getNavStr(FN);
		if("1".equals(opt)){
			this.SysUser = sysUserMgr.getUser(request().getParameter("t"));
		}
		return "oprt";
	}
	
	public String t;
	public String role() {
		this.navStr = function.getNavStr(FN);
		if("2".equals(opt)){
			this.sysFuncData = function.getFuncProperties();
			this.accessRightData = function.getLookup("ACCESS_RIGHT");
			this.tmpltAuthMap = function.getMapTemplateAuthorities(request().getParameter("t"));
			this.t = request().getParameter("t");
		}
		return "role";
	}
	
	public String setAuth(){
		Object o = session().get("user");
		String userCode = "";
		if (o != null) {
			userCode = ((SysUser) o).getUserCode();
		}
		try {
			function.modifyAuthorities(request(), userCode);
		} catch (RuntimeException rx) {
			System.out.println(rx.getMessage());
		}
		
		return "role";
	}
	
	public String getAll(){
		this.sysTemplates = sysTemplateMgr.getSysTemplates();
		this.sysUsers = sysUserMgr.getSysUsers();
		this.departments = departmentsMgr.getDepartments();
		this.staffs = staMgr.findStaff();
		return "SUCCESS";
	}
	
	public String getUserByCode(){
		try {
			un = new String(un.getBytes("ISO8859-1"),"UTF-8");
		
			this.sysUsers = sysUserMgr.getUserByCode(un);
			this.departments = departmentsMgr.getDepartments();
			this.sysTemplates = sysTemplateMgr.getSysTemplates();
			this.staffs = staMgr.findStaff();
		
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		return "SUCCESS";
	}
	
	public String getByName(){
		System.out.println(un);
		System.out.println(qm);
		if(qm.equals("2")){
			this.sysUsers = sysUserMgr.getUserByCode(un);
			this.departments = departmentsMgr.getDepartments();
			this.sysTemplates = sysTemplateMgr.getSysTemplates();
			this.staffs = staMgr.findStaff();
		}else if(qm.equals("1")){
			this.sysUsers = sysUserMgr.getUserByName(un);
			this.departments = departmentsMgr.getDepartments();
			this.sysTemplates = sysTemplateMgr.getSysTemplates();
			this.staffs = staMgr.findStaff();
		}else if(qm.equals("3")){
			this.staffs = staMgr.getStaffByName(un);
			this.sysUsers = sysUserMgr.getUserByStaff(staffs);
			this.departments = departmentsMgr.getDepartments();
			this.sysTemplates = sysTemplateMgr.getSysTemplates();
		}else if(qm.equals("4")){
			this.sysTemplates = sysTemplateMgr.getSysTemplatesByName(un);
			this.sysUsers = sysUserMgr.getUserByTpl(sysTemplates);
			this.departments = departmentsMgr.getDepartments();
			this.staffs = staMgr.findStaff();
		}else if(qm.equals("5")){
			this.departments = departmentsMgr.getDepartmentsByName(un);
			this.sysTemplates = sysTemplateMgr.getSysTemplates();
			this.sysUsers = sysUserMgr.getUserByDep(departments);
			this.staffs = staMgr.findStaff();
		}else if(qm.equals("0")){
			this.sysTemplates = sysTemplateMgr.getSysTemplates();
			this.sysUsers = sysUserMgr.getSysUsers();
			this.departments = departmentsMgr.getDepartments();
			this.staffs = staMgr.findStaff();
			return "SUCCESS";
		}
		return "SUCCESS";
	}
	public String updateUser(){
		System.out.println("zzzzzzzzzzzzzzz"+qm+un+staffname+templatesname+Departmentsname+sas);
		this.sysUsers = sysUserMgr.getUserByCode(qm);
		this.staffs = staMgr.getStaffByName(staffname);
		System.out.println(sysUsers.get(0));
//		this.setStaff(staMgr.getById(staffname));
//		this.setSysTemplate(sysTemplateMgr.getById(templatesname));
//		this.department = departmentsMgr.getById(Departmentsname);
		sysUsers.get(0).setDeptCode(Departmentsname);
		sysUsers.get(0).setTemplateCode(templatesname);
		sysUsers.get(0).setStaid(staffs.get(0).getStaid());
		sysUsers.get(0).setUserName(un);
		sysUsers.get(0).setStatus(sas);
		sysUserMgr.updateUser(sysUsers.get(0));
		return "SUCCESS";
	}
	public String deleteUser(){
		System.out.println(deleteUsers.get(0)+"   "+deleteUsers.size());
		sysUserMgr.deleteUser(deleteUsers);
		this.sysTemplates = sysTemplateMgr.getSysTemplates();
		this.sysUsers = sysUserMgr.getSysUsers();
		this.departments = departmentsMgr.getDepartments();
		this.staffs = staMgr.findStaff();
		return "SUCCESS";
	}
	public String getPword() {
		return pword;
	}
	public void setPword(String pword) {
		this.pword = pword;
	}
	public String getStid() {
		return stid;
	}
	public void setStid(String stid) {
		this.stid = stid;
	}
	public String getSttus() {
		return sttus;
	}
	public void setSttus(String sttus) {
		this.sttus = sttus;
	}
	private String pword;
	private String depid;
	private String tmpCode;
	private String stid;
	private String sttus;
	private Date currentTime;
	private SimpleDateFormat formatter;
	public String addUser(){
		System.out.println(un+"-----"+uc+"-----"+depid+"-----"+tmpCode+"-----"+stid+"-----"+sttus);
		try{
			this.staffs = staMgr.getStaffByName(stid);
			sysUser = new SysUser();
			sysUser.setUserCode(uc);
			sysUser.setUserName(un);
			sysUser.setPassword(MD5.toMD5("123456"));
			sysUser.setTemplateCode(tmpCode);
			sysUser.setStaid(staffs.get(0).getStaid());
			sysUser.setDeptCode(depid);
			sysUser.setStatus(sttus);
			currentTime = new Date();
	//		formatter = new SimpleDateFormat("yyyy-MM-dd");
	//		String dateString = formatter.format(currentTime);
			sysUser.setLastChgPwd(currentTime);
			sysUser.setMustChgPwd("Y");
			this.sysUsers = sysUserMgr.getSysUsers();
			sysUser.setSeq(function.getMaxSeq("SYS_USER_MST"));
			System.out.println(sysUser);
			sysUserMgr.saveUser(sysUser);
		} catch (RuntimeException rx){
			msg = getText("save.failure");
			rx.printStackTrace();
		}
		if (msg == null || "".equals(msg.trim())) {
			msg = getText("save.success");
		}
		this.sysTemplates = sysTemplateMgr.getSysTemplates();
		this.sysUsers = sysUserMgr.getSysUsers();
		this.departments = departmentsMgr.getDepartments();
		this.staffs = staMgr.findStaff();
		return "SUCCESS";
	}
//	public String doOprt() {
//		Object o = session().get("user");
//		String userCode = "";
//		if (o != null) {
//			userCode = ((SysUser) o).getUserCode();
//		}
//		if ("0".equals(opt)) {
//			if (sysTemplate.getCrtBy() == null
//					|| "".equals(sysTemplate.getCrtBy())) {
//				sysTemplate.setCrtBy(userCode);
//			}
//			if (sysTemplate.getCrtDate() == null
//					|| "".equals(sysTemplate.getCrtDate())) {
//				sysTemplate.setCrtDate(new Date());
//			}
//			sysTemplate.setSeq(function.getMaxSeq("SYS_TEMPLATE_MST"));
//			sysUserMgr.addSysTemplate(sysTemplate);
//		} else {
//			sysTemplate.setUpdBy(userCode);
//			sysTemplate.setUpdDate(new Date());
//			sysUserMgr.updSysTemplate(sysTemplate);
//		}
//
//		return list();
//	}
	
//	public String del(){
//		String t = request().getParameter("t");
//		if (t != null && !"".equals(t)) {
//			sysUserMgr.delSysTemplateByIds(t.split(","));
//		}
//		return list();
//	}

	public void setSysTemplate(SysTemplate sysTemplate) {
		this.sysTemplate = sysTemplate;
	}
	public SysTemplate getSysTemplate() {
		return sysTemplate;
	}
	public void setStaff(Staff staff) {
		this.staff = staff;
	}
	public Staff getStaff() {
		return staff;
	}
	@Resource
	private SysUserMgr sysUserMgr;
	
	@Resource
	private Function function;
	
	@Resource
	private DepartmentsMgr departmentsMgr;
	
	@Resource
	private StaMgr staMgr;

	@Resource
	private SysTemplateMgr sysTemplateMgr;
	
	@Resource
	private StaffService staffService;
	

}
